<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/5/3
 * Time: 上午9:34
 */
namespace Common\Model;
use Think\Model;

class CollectionModel extends Model{
    protected $table;
    protected $prefix;
    protected $collectiontable;

    /** 初始化数据
     * @param string $collectiontable
     * @param string $prefix
     */
    public function __construct($collectiontable,$prefix =""){
        parent::__construct();
        $this -> prefix = empty($prefix) ? C('DB_PRIFIX') : $prefix;
        $this -> table  = $collectiontable;
        $this -> collectiontable = $prefix . $collectiontable;
    }

    public function getList(){
        $info = M($this -> table) -> field('dx_collection.id,dx_collection.pid,name,number,author,age,material,size,weight,pubdate,content,keywords,dx_collection.status,cates,columns')->join('left join dx_columns c on c.id = dx_collection.pid')-> select();
        return $info;
    }

    /**添加藏品
     * @param $data
     * @return array|bool
     */
    public function addC($data){
        $check = $this -> checkInfo($data);
        if($check !== true){
            return $check;
        }
        $arr = $this->formatData($data);
        $result = M($this -> table) -> add($arr,[],true);
        if($result){
            return $this->returnInfo('操作成功！');
        }else{
            return $this->returnInfo('操作失败！','',0);
        }
    }

    /**检查数据
     * @param $data
     * @return array|bool
     */

    public function checkInfo($data){
        if(empty($data['name'])){
            return $this -> returnInfo('藏品名称不能为空！','',0);
        }
        $collection = M($this -> table);
        $isBE = $collection -> where(['name' => $data['name']]) -> find();
        if($isBE){
            return $this->returnInfo('藏品信息已存在！','',0);
        }
        return true;
    }
    /**格式化数据
     * @param $data
     * @return array
     */
    public function formatData($data){
        $name = empty($data['name'])? '':$data['name'];
        $pid = empty($data['pid'])? '':$data['pid'];
        $number = empty($data['number'])? '':$data['number'];
        $cates = empty($data['cates'])? 0:$data['cates'];
        $author = empty($data['author'])? '':$data['author'];
        $age = empty($data['age'])? '':$data['age'];
        $material = empty($data['material'])? '':$data['material'];
        $size = empty($data['size'])? '':$data['size'];
        $weight = empty($data['weight'])? '':$data['weight'];
        $pubdate = empty($data['pubdate'])? '':$data['pubdate'];
        $litpic = empty($data['litpic'])? '':$data['litpic'];
        $content = empty($data['content'])? '':$data['content'];
        $keywords = empty($data['keywords'])? '':$data['keywords'];
        $status = empty($data['status'])? 1:$data['status'];
        $arr = ['name' => $name,'pid' => $pid ,'number' => $number,'cates' => $cates,'author' => $author,'age' => $age,'material' => $material,'size' => $size,'weight' => $weight,'pubdate' => $pubdate,'litpic' => $litpic,'content' => $content,'keywords' => $keywords,'status' => $status];
        return $arr;
    }

    /**返回信息操作
     * @param string $msg
     * @param $data
     * @param int $status
     * @return array
     */
    public function returnInfo($msg = "", $data, $status = 1){
        return ['msg' => $msg, 'data' => $data, 'status' => $status];
    }

    /**删除藏品
     * @param $id
     * @return array
     */
    public function del($id){
        if(empty($id)){
            return $this -> returnInfo('删除异常！','非法操作！','error');
        }
        $menu = M($this -> table);
        $result = $menu -> where(['id' => $id]) -> delete();
        if($result){
            return $this->returnInfo('您已经永久删除了这件藏品！','删除成功!','success');
        }else{
            return $this->returnInfo('非法操作！','删除失败！','error');
        }
    }

    /**修改藏品状态
     * @param $id
     * @return array
     */
    public function change($id){
        if(empty($id)){
            return $this -> returnInfo('删除异常！','非法操作！','error');
        }
        $menu = M($this -> table);
        $menuState = $menu-> field('status') -> where(['id' => $id]) -> find();
        if($menuState['status'] == 0){
            $menu -> where(['id' => $id]) -> setField(['status' => 1]);
            $result = $this -> returnInfo('已开启',1);
        }else{
            $menu -> where(['id' => $id]) -> setField(['status' => 0]);
            $result = $this -> returnInfo('已禁用',0);
        }
        return $result;
    }

    /**获取指定ID信息
     * @param $id
     * @return array|mixed
     */
    public function getInfo($id){
        if(empty($id)){
            return $this -> returnInfo('操作异常！','0');
        }
       $info = M($this -> table) -> where(['id' => $id]) -> find();
        return $info;
    }
    public function saveC($data){
        if(empty($data['pubdate'])){
            return $this->returnInfo('时间格式错误！','',0);
        }
        $pic = M($this -> table) -> field('litpic') -> where(['id' => $data['id']]) -> find();
        $data['litpic'] = $pic['litpic'] . $data['litpic'];
        $result = M($this -> table) -> add($data,[],true);
        if($result){
            return $this->returnInfo('操作成功！');
        }else{
            return $this->returnInfo('操作失败！','',0);
        }
    }
}